﻿import { STATUS_DIC, SHOW_STATUS_DIC } from "@/utils/const";

export default () => {
  // 生成 value -> label 的映射表
  const toMap = dic => Object.fromEntries(dic.map(i => [i.value, i.label]));
  const STATUS_MAP = toMap(STATUS_DIC);
  const SHOW_STATUS_MAP = toMap(SHOW_STATUS_DIC);

  // 搜索配置
  const searchOption = ref({
    formConfig: {
      title: { label: "公告标题", component: "el-input" },
      createTime: {
        label: "新增时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      sendTime: {
        label: "发送时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      updateTime: {
        label: "编辑时间",
        component: "el-date-picker",
        type: "daterange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD",
      },
      updateName: { label: "操作人员", component: "el-input" },

      // 发送状态（用同一份字典）
      status: {
        label: "发送状态",
        component: "el-select",
        dicData: STATUS_DIC,
        dicProps: { label: "label", value: "value" },
        placeholder: "请选择发送状态",
        clearable: true,
      },
      // 展示状态
      showStatus: {
        label: "展示状态",
        component: "el-select",
        dicData: SHOW_STATUS_DIC,
        dicProps: { label: "label", value: "value" },
        placeholder: "请选择展示状态",
        clearable: true,
      },
    },
  });

  const toYesNo = v => (String(v) === "1" || v === 1 ? "是" : "否");

  // 表格配置
  const tableOption = ref(
    Utils.tools.deleteSearchConfig({
      dialogWidth: "30%",
      editBtn: false,
      delBtn: false,
      menuWidth: 52 * 4 + 8,
      align: "left",
      column: {
        title: { label: "公告标题", minWidth: 160, display: false },
        targetName: { label: "触达对象", minWidth: 160, display: false },
        // isScroll: { label: "是否滚动", minWidth: 120, display: false, formatter: row => toYesNo(row.isScroll) },
        createTime: {
          label: "新增时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        sendTime: {
          label: "发送时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        updateTime: {
          label: "最近编辑时间",
          minWidth: 170,
          type: "datetime",
          valueFormat: "YYYY-MM-DD HH:mm:ss",
          addDisplay: false,
          editDisplay: false,
        },
        updateName: { label: "操作人员", minWidth: 120, addDisplay: false, editDisplay: false },

        status: {
          label: "发送状态",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
          formatter: row => STATUS_MAP[row.status] ?? row.status ?? "-",
        },
        showStatus: {
          label: "展示状态",
          minWidth: 120,
          addDisplay: false,
          editDisplay: false,
          formatter: row => {
            // 未发送或发送失败时，展示状态显示"/"
            const sendStatus = String(row.status || "").toUpperCase();
            if (sendStatus === "INIT" || sendStatus === "FAIL") {
              return "/";
            }
            return SHOW_STATUS_MAP[row.showStatus] ?? row.showStatus ?? "-";
          },
        },
      },
    })
  );

  return { searchOption, tableOption };
};
